package com.example.pagingdemo.paging.doctor;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.DataSource;
import androidx.paging.PageKeyedDataSource;

import com.example.pagingdemo.api.Api;
import com.example.pagingdemo.api.RetrofitClient;
import com.example.pagingdemo.model.ApiResponse;
import com.example.pagingdemo.model.DoctorVo;
import com.example.pagingdemo.model.UserResponse;
import com.example.pagingdemo.paging.AbsViewModel;
import com.example.pagingdemo.paging.PageConfig;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: tuyrt7 on 2020/10/27.
 */
public class DoctorViewModel extends AbsViewModel<DoctorVo.DocArrBean> {

    private AtomicBoolean loadAfter = new AtomicBoolean(false);
    private int mPageIndex = 1;
    int client_id = 1;

    public int getPageIndex() {
        return mPageIndex;
    }


    @Override
    protected DataSource createDataSource() {
        return new DoctorDataSource();
    }

    class DoctorDataSource extends PageKeyedDataSource<Integer, DoctorVo.DocArrBean> {

        @Override
        public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, DoctorVo.DocArrBean> callback) {
            loadData(1, callback, null);
        }

        @Override
        public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, DoctorVo.DocArrBean> callback) {
            callback.onResult(Collections.emptyList(), 0);
        }

        @Override
        public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, DoctorVo.DocArrBean> callback) {
            loadData(params.key, null, callback);
        }
    }


    private void loadData(int pageIndex, PageKeyedDataSource.LoadInitialCallback<Integer, DoctorVo.DocArrBean> initCallback, @NonNull PageKeyedDataSource.LoadCallback<Integer,DoctorVo.DocArrBean> callback) {
        mPageIndex = pageIndex;
        if (pageIndex > 1) {
            loadAfter.set(true);
        }

        RetrofitClient.get().buildApi(Api.BASE_URL4)
                .getDoctorList(client_id, pageIndex,9)
                .enqueue(new Callback<ApiResponse<DoctorVo>>() {
                    @Override
                    public void onResponse(Call<ApiResponse<DoctorVo>> call, Response<ApiResponse<DoctorVo>> response) {
                        if (response != null && response.body().data.doc_arr != null) {
                            List<DoctorVo.DocArrBean> arrBeanList = response.body().data.doc_arr;
                            Log.d("aaaa", "getDoctorList获得数据: " + response.body().data);
                            if (initCallback != null) {
                                initCallback.onResult(arrBeanList, null, pageIndex + 1);
                            } else {
                                callback.onResult(arrBeanList, pageIndex + 1);
                            }

                            if (pageIndex > 1) {
                                //通过BoundaryPageData发送数据 告诉UI层 是否应该主动关闭上拉加载分页的动画
                                ((MutableLiveData) getBoundaryPageData()).postValue(arrBeanList.size() > 0);
                                loadAfter.set(false);
                            }
                            mPageIndex = pageIndex + 1;
                        }
                    }

                    @Override
                    public void onFailure(Call<ApiResponse<DoctorVo>> call, Throwable t) {

                    }
                });

        Log.e("loadData", "===加载=== page:" + pageIndex);
    }

    public void loadAfter(int pageIndex, PageKeyedDataSource.LoadCallback<Integer, DoctorVo.DocArrBean> callback) {
        Log.d("TAG", "loadAfter: pageIndex" + pageIndex);
        //是否加载更多的表示位
        if (loadAfter.get()) {
            callback.onResult(Collections.emptyList(), 0);
            return;
        }

        loadData(pageIndex, null, callback);
    }
}
